import 'dart:convert';

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_hotwork/bean/QuestionListBean.dart';
import 'package:flutter_hotwork/homepage/BaseTitleBar.dart';
import 'package:flutter_hotwork/homepage/BaseViewBar.dart';
import 'package:flutter_hotwork/utils/DioUtils.dart';
import 'package:flutter_hotwork/xunjian/XunJianWenTiShangBaoDetails.dart';

import '../EventBusUtils.dart';

class GuzhangBaogaoScreen extends StatefulWidget {
  @override
  GuzhangBaogaoState createState() => new GuzhangBaogaoState();
}

QuestionListBean zhifalistbeas = new QuestionListBean();
bool hasdata = false;

class GuzhangBaogaoState extends State<GuzhangBaogaoScreen> {
  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
  }

  ///当以来的State对象改变时会调用
  @override
  void didChangeDependencies() {
    print('------didChangeDependencies-------');
    super.didChangeDependencies();
  }

  @override
  void initState() {
    print('-----initState-----');
    super.initState();
    getHotUserCount();
    bus.on("flush", (arg) {
      getHotUserCount();
    });
  }

  var bus = EventBus();

  @override
  void deactivate() {
    super.deactivate();

    /// 必须在deactivate方法中声明 才可以保证事件不会因为bus移除事件而无法推送
  }

  @override
  Widget build(BuildContext context) {
    ScrollController controller = ScrollController();
    return Scaffold(
      appBar: new BaseViewBar(
          childView: new BaseTitleBar(
            "故障报告",
            leftIcon: '',
            rightIcon: '',
            rightClick: () {
              // print("点击了右边干嘛啊。。。哦");
              //
              // Navigator.push(context, MaterialPageRoute(builder: (context) {
              //   return new ZhifaEditScreen();
              // }));
            },
            leftClick: () {
              print("点击了左边干嘛啊。。。哦");
            },
          ),
          preferredSize: Size.fromHeight(50.0)),
      body: Container(
        child: Column(
          children: [
            hasdata
                ? Expanded(
                    child: RefreshIndicator(
                      onRefresh: () async {
                        setState(() {
                          getHotUserCount();
                        });
                      },
                      child: showList()
                          ? genContentList(
                              zhifalistbeas.result, context, controller)
                          : Container(),
                    ),
                  )
                : Container(),
          ],
        ),
      ),
    );
  }

  void getHotUserCount() async {
    try {
      Response response;
      response = await DioUtils.dio.get(DioUtils.API_PREFIX +
          "/checkQuestion/listCheckQuestion?status=0&isAllocated=0");
      Map respMap = json.decode(response.data);
      zhifalistbeas = QuestionListBean.fromJson(respMap);
      hasdata = true;
      setState(() {});
      print('响应数据：' + response.toString());
    } catch (e) {
      //  LocalToast.toast(context,msg: "网络错误");
      print('请求出错：' + e.toString());
    }
  }
}

bool showList() {
  if (null != zhifalistbeas.result && zhifalistbeas.result.length > 0) {
    return true;
  } else {
    return false;
  }
}

genContentList(List<Question> data, BuildContext context, controller) {
  double rpx = MediaQuery.of(context).size.width / 750;
  return ListView.builder(
      shrinkWrap: true,
      controller: controller,
      physics: new AlwaysScrollableScrollPhysics(),
      itemCount: data.length,
      itemBuilder: (BuildContext context, int index) {
        return Container(
          decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.all(Radius.circular(5)),
            boxShadow: <BoxShadow>[
              //设置阴影
              new BoxShadow(
                color: Colors.grey, //阴影颜色
                blurRadius: 10.0, //阴影大小
              ),
            ],
          ),

          margin: EdgeInsets.only(left: 10, top: 10, right: 10, bottom: 10),
          // height: 160 * rpx,
          child: Column(
            children: <Widget>[
              Container(
                height: 10,
              ),
              Container(
                padding:
                    EdgeInsets.only(left: 10, top: 0, right: 10, bottom: 0),
                child: Row(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    Container(
                      width: 10,
                    ),
                    Text(
                      data[index].createTime,
                      style: new TextStyle(
                        color: Color(0xFF333333),
                        fontSize: 12.0,
                      ),
                    ),
                    Flexible(fit: FlexFit.tight, child: SizedBox()),
                    Container(
                        height: 20,
                        padding: EdgeInsets.only(
                            left: 5, top: 0, right: 5, bottom: 0),
                        child: data[index].status == 1
                            ? Text(
                                "已解决",
                                style: new TextStyle(
                                  color: Colors.green,
                                  fontSize: 12.0,
                                ),
                              )
                            : Text(
                                "未解决",
                                style: new TextStyle(
                                  color: Colors.red,
                                  fontSize: 12.0,
                                ),
                              )),
                  ],
                ),
              ),
              Container(
                height: 1,
                margin: EdgeInsets.only(left: 10, top: 5, right: 10, bottom: 0),
                color: Colors.grey[300],
              ),
              // Container(
              //   padding:
              //       EdgeInsets.only(left: 10, top: 0, right: 10, bottom: 0),
              //   margin: EdgeInsets.only(left: 0, top: 5, right: 0, bottom: 0),
              //   child: Row(
              //     children: [
              //       // Image(height:20,width: 20,image: AssetImage('images/mingcheng.png')),
              //       // Image(height:20,width: 20,image: AssetImage('images/weizhi.png')),
              //       Container(
              //         width: 10,
              //       ),
              //       Expanded(
              //         child: Text(
              //           "地点：" +
              //               (null != data[index].questionAddress
              //                   ? data[index].questionAddress
              //                   : ""),
              //           maxLines: 100,
              //           style: new TextStyle(
              //             color: Color(0xFF333333),
              //             fontSize: 12.0,
              //           ),
              //         ),
              //       ),
              //     ],
              //   ),
              // ),
              // Container(
              //   padding:
              //       EdgeInsets.only(left: 10, top: 0, right: 10, bottom: 0),
              //   margin: EdgeInsets.only(left: 0, top: 5, right: 0, bottom: 0),
              //   child: Row(
              //     children: [
              //       // Image(height:20,width: 20,image: AssetImage('images/mingcheng.png')),
              //       // Image(height:20,width: 20,image: AssetImage('images/weizhi.png')),
              //       Container(
              //         width: 10,
              //       ),
              //       Expanded(
              //         child: Text(
              //           "项目名称：" +
              //               (null != data[index].projectName
              //                   ? data[index].projectName
              //                   : ""),
              //           maxLines: 100,
              //           style: new TextStyle(
              //             color: Color(0xFF333333),
              //             fontSize: 12.0,
              //           ),
              //         ),
              //       ),
              //     ],
              //   ),
              // ),
              // Container(
              //   padding:
              //       EdgeInsets.only(left: 10, top: 0, right: 10, bottom: 0),
              //   margin: EdgeInsets.only(left: 0, top: 5, right: 0, bottom: 5),
              //   child: Row(
              //     children: [
              //       // Image(height:20,width: 20,image: AssetImage('images/mingcheng.png')),
              //       Container(
              //         width: 10,
              //       ),
              //       Expanded(
              //         child: Text(
              //           "描述：" + data[index].questionDescription,
              //           maxLines: 100,
              //           style: new TextStyle(
              //             color: Color(0xFF333333),
              //             fontSize: 12.0,
              //           ),
              //         ),
              //       ),
              //     ],
              //   ),
              // ),
              Column(
                children: [
                  Container(
                    padding:
                        EdgeInsets.only(left: 10, top: 0, right: 10, bottom: 0),
                    margin:
                        EdgeInsets.only(left: 0, top: 5, right: 0, bottom: 0),
                    child: Row(
                      children: [
                        Container(
                          width: 10,
                        ),
                        Text(
                          "人员姓名:",
                          style: new TextStyle(
                            color: Color(0xFF333333),
                            fontSize: 12.0,
                          ),
                        ),
                        Text(
                          data[index].checkUserName,
                          style: new TextStyle(
                            color: Color(0xFF333333),
                            fontSize: 12.0,
                          ),
                        ),
                      ],
                    ),
                  ),
                  Container(
                    padding:
                        EdgeInsets.only(left: 10, top: 0, right: 10, bottom: 0),
                    margin:
                        EdgeInsets.only(left: 0, top: 5, right: 0, bottom: 0),
                    child: Row(
                      children: [
                        Container(
                          width: 10,
                        ),
                        Text(
                          "区域名称:",
                          style: new TextStyle(
                            color: Color(0xFF333333),
                            fontSize: 12.0,
                          ),
                        ),
                        Text(
                          data[index].areaName,
                          style: new TextStyle(
                            color: Color(0xFF333333),
                            fontSize: 12.0,
                          ),
                        ),
                      ],
                    ),
                  ),
                  Container(
                    padding:
                        EdgeInsets.only(left: 10, top: 0, right: 10, bottom: 0),
                    margin:
                        EdgeInsets.only(left: 0, top: 5, right: 0, bottom: 0),
                    child: Row(
                      children: [
                        Container(
                          width: 10,
                        ),
                        Text(
                          "设备名称:",
                          style: new TextStyle(
                            color: Color(0xFF333333),
                            fontSize: 12.0,
                          ),
                        ),
                        Text(
                          data[index].deviceName,
                          style: new TextStyle(
                            color: Color(0xFF333333),
                            fontSize: 12.0,
                          ),
                        ),
                      ],
                    ),
                  ),
                  Container(
                    padding:
                        EdgeInsets.only(left: 10, top: 0, right: 10, bottom: 0),
                    margin:
                        EdgeInsets.only(left: 0, top: 5, right: 0, bottom: 0),
                    child: Row(
                      children: [
                        Container(
                          width: 10,
                        ),
                        Text(
                          "问题项目:",
                          style: new TextStyle(
                            color: Color(0xFF333333),
                            fontSize: 12.0,
                          ),
                        ),
                        Text(
                          data[index].projectName,
                          style: new TextStyle(
                            color: Color(0xFF333333),
                            fontSize: 12.0,
                          ),
                        ),
                      ],
                    ),
                  ),
                  Container(
                    padding:
                        EdgeInsets.only(left: 10, top: 0, right: 10, bottom: 0),
                    margin:
                        EdgeInsets.only(left: 0, top: 5, right: 0, bottom: 0),
                    child: Row(
                      children: [
                        Container(
                          width: 10,
                        ),
                        Text(
                          "问题地点:",
                          style: new TextStyle(
                            color: Color(0xFF333333),
                            fontSize: 12.0,
                          ),
                        ),
                        Text(
                          data[index].questionAddress != null
                              ? data[index].questionAddress
                              : "",
                          style: new TextStyle(
                            color: Color(0xFF333333),
                            fontSize: 12.0,
                          ),
                        ),
                      ],
                    ),
                  ),
                  Container(
                    padding:
                        EdgeInsets.only(left: 10, top: 0, right: 10, bottom: 0),
                    margin:
                        EdgeInsets.only(left: 0, top: 5, right: 0, bottom: 0),
                    child: Row(
                      children: [
                        Container(
                          width: 10,
                        ),
                        Text(
                          "任务名称:",
                          style: new TextStyle(
                            color: Color(0xFF333333),
                            fontSize: 12.0,
                          ),
                        ),
                        Text(
                          data[index].checkName,
                          style: new TextStyle(
                            color: Color(0xFF333333),
                            fontSize: 12.0,
                          ),
                        ),
                      ],
                    ),
                  ),
                  Container(
                    padding:
                        EdgeInsets.only(left: 10, top: 0, right: 10, bottom: 0),
                    margin:
                        EdgeInsets.only(left: 0, top: 5, right: 0, bottom: 0),
                    child: Row(
                      children: [
                        Container(
                          width: 10,
                        ),
                        Text(
                          "描述:",
                          style: new TextStyle(
                            color: Color(0xFF333333),
                            fontSize: 12.0,
                          ),
                        ),
                        Text(
                          data[index].questionDescription,
                          style: new TextStyle(
                            color: Color(0xFF333333),
                            fontSize: 12.0,
                          ),
                        ),
                      ],
                    ),
                  ),
                ],
              ),
              Container(
                height: 1,
                margin: EdgeInsets.only(left: 10, top: 5, right: 10, bottom: 0),
                color: Colors.grey[300],
              ),
              InkWell(
                onTap: () {
                  Navigator.push(context, MaterialPageRoute(builder: (context) {
                    // MyApplication.areaDeviceBean2 = itemList[position];
                    return new XunJianWenTiShangBaoDetails(data[index].id);
                  }));
                },
                child: Container(
                  padding:
                      EdgeInsets.only(left: 200, right: 0, top: 0, bottom: 0),
                  child: ButtonTheme(
                    minWidth: 100.0, //设置最小宽度
                    height: 20.0,
                    child: RaisedButton(
                      // onPressed: _login,
                      child: Text(
                        '查看详情',
                        style: new TextStyle(color: Colors.white),
                      ),
                      color: Colors.blue,
                      shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.all(Radius.circular(15)),
                      ),
                    ),
                  ),
                ),
              ),
            ],
          ),
        );
      });
}
